package com.lz.ionicbase.ui.activity;

import android.annotation.TargetApi;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.bumptech.glide.Glide;
import com.lz.ionicbase.SuperApp;
import com.lz.ionicbase.utils.SpUtils;
import com.readystatesoftware.systembartint.SystemBarTintManager;

import java.util.Locale;

/**
 * Created by lenovo on 2017/2/4.
 */

public abstract class BaseActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getActivityLayout());
        initView();
        initListener();
        removeCache();

        // 4.4及以上版本开启
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setTranslucentStatus(true);
        }

        SystemBarTintManager tintManager = new SystemBarTintManager(this);
        tintManager.setStatusBarTintEnabled(true);
        tintManager.setNavigationBarTintEnabled(true);
        tintManager.setTintColor(Color.parseColor("#E0E0E0"));

//        hideNavigation();
        onSystemLanChange();
    }

    /**
     * 用户设置系统语言设置后，应用会跟随系统。所以要重新设置语言
     */
    public void onSystemLanChange() {
        SuperApp app = (SuperApp) getApplication();
        Locale currentSysLocale = app.getResources().getConfiguration().locale;
        String lan  = SpUtils.getString(getApplicationContext(), SpUtils.KEY_LANGUAGE,"简体中文");

        Locale spLan = (Locale) SuperApp.lanMap.get(lan)[1];;

        if(!currentSysLocale.equals(spLan)){
            //系统语言设置和应用内的语言设置不同
            Resources resources = getApplicationContext().getResources();
            DisplayMetrics dm = resources.getDisplayMetrics();
            Configuration config = resources.getConfiguration();
            config.locale = spLan;
            resources.updateConfiguration(config, dm);
            finish();
        }
    }

    /**
     * 隐藏底部导航栏
     */
    private void hideNavigation() {
        //让虚拟键盘一直不显示
        Window window = getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION|View.SYSTEM_UI_FLAG_IMMERSIVE;
        window.setAttributes(params);
    }

    private void removeCache() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Glide.get(getApplication()).clearDiskCache();
                Glide.get(getApplication()).clearDiskCache();
            }
        });

    }

    @TargetApi(19)
    private void setTranslucentStatus(boolean on) {
        Window win = getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }

    @Override
    protected void onResume() {
        /**
         * 设置为横屏
         */
        if(getRequestedOrientation()!= ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE){
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
        super.onResume();
    }

    protected abstract int getActivityLayout();

    protected abstract void initView();

    protected abstract void initListener() ;
}
